package com.yunhe.marketing.controller;

import com.yunhe.common.annotation.OperationRecord;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.util.HeaderUtil;
import com.yunhe.marketing.domain.Enterprise;
import com.yunhe.marketing.service.ITaskService;
import com.yunhe.marketing.util.MyHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: DongWenYu
 * @Date: 2021/2/26 13:52
 */
@Api(value = "task", description = "任务栏维护", tags = {"Model_Task"})
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    ITaskService taskService;

    @Autowired
    MyHeaderUtil headerUtil;

    @ApiOperation("分页展示所分配的线索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页展示的条数", required = true, dataType = "int", paramType = "query")
    })
    @GetMapping("/responsibleClue")
    public ResponseEntity<ResultObject> selectResponsibleClue(@RequestParam("page") int page,
                                                            @RequestParam("size") int size) throws ArgumentErrorException {
        Long responsiblePersonId = headerUtil.getHeaderUser();
        if(responsiblePersonId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        PageResult result = taskService.selectResponsibleClue(responsiblePersonId,page,size);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("分页展示所分配的需求")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "responsiblePersonId", value = "负责人ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页展示的条数", required = true, dataType = "int", paramType = "query")
    })
    @GetMapping("/responsibleDemand")
    public ResponseEntity<ResultObject> selectResponsibleDemand(@RequestParam("page") int page,
                                                            @RequestParam("size") int size) throws ArgumentErrorException {
        Long responsiblePersonId = headerUtil.getHeaderUser();
        if(responsiblePersonId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        PageResult result = taskService.selectResponsibleDemand(responsiblePersonId,page,size);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("反馈信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "企业ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "responsibleId", value = "业务员ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "feedbackStatus", value = "反馈状态", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "conversationContent", value = "交谈内容", required = false, dataType = "String", paramType = "query")
    })
    @PostMapping("/feedback")
    @OperationRecord(menu = "线索需求库管理", actionType = SystemConsts.ACTION.EDIT)
    public ResponseEntity<ResultObject> selectResponsibleDemand(@RequestParam("id") Long id,
                                                                @RequestParam("feedbackStatus") String feedbackStatus,
                                                                @RequestParam(value = "conversationContent" , required = false) String conversationContent) throws ArgumentErrorException {
        Long responsiblePersonId = headerUtil.getHeaderUser();
        if(responsiblePersonId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        Enterprise result = taskService.feedBack(id,responsiblePersonId,feedbackStatus,conversationContent);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }
}
